/**
 * AluiCommons -  http://code.google.com/p/alui-toolbox/
 * Copyright (c) 2008-2009
 * by Fabien Sanglier
 * 
 * This file is part of AluiCommons.
 * 
 * AluiCommons is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AluiCommons is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AluiCommons.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */

using System;
using com.plumtree.xpshared.config;
using com.plumtree.server;
using com.plumtree.openkernel.config;
using com.plumtree.openkernel.factory;
using com.plumtree.openfoundation.security;

namespace AluiServerLib.Helper
{
	/// <summary>
	/// Summary description for NativeSessionMgr.
	/// </summary>
	public class NativeApplicationMgr
	{
		private static NativeApplicationMgr m_SingleInstance;
		private const string DEFAULT_APP_NAME = "portal";
		
		private NativeApplicationMgr()
		{}

		public static NativeApplicationMgr GetInstance()
		{
			if(null == m_SingleInstance)
			{
				m_SingleInstance = new NativeApplicationMgr();
				m_SingleInstance.InitApplication(DEFAULT_APP_NAME);
			}
			return m_SingleInstance;
		}

		public IPTSession GetNativeSession(string loginToken)
		{
			IPTSession ptsession = PortalObjectsFactory.CreateSession();
			ptsession.Reconnect(loginToken);
			return ptsession;
		}
        
		public IPTSession GetNativeSession(string username, string pwd, bool isPasswordEncrypted, Object oSecurityProviderInfo)
		{
			IPTSession ptsession = PortalObjectsFactory.CreateSession();

            if (isPasswordEncrypted)
                ptsession.Connect(username, XPCrypto.GetInstance(XPCryptoType.RC2_40).Decrypt(pwd, username), oSecurityProviderInfo);
            else
                ptsession.Connect(username, pwd, oSecurityProviderInfo); 

            return ptsession;
		}

        public IPTSession GetNativeSession(int userid, string pwd, bool isPasswordEncrypted, Object oSecurityProviderInfo)
        {
            IPTSession ptsession = PortalObjectsFactory.CreateSession();

            if (isPasswordEncrypted)
                ptsession.Connect(userid, XPCrypto.GetInstance(XPCryptoType.RC2_40).Decrypt(pwd, userid.ToString()), oSecurityProviderInfo);
            else
                ptsession.Connect(userid, pwd, oSecurityProviderInfo);

            return ptsession;
        }

        public IPTSession GetGuestSession()
        {
            IPTSession ptGuestSession = PortalObjectsFactory.CreateSession();
            ptGuestSession.Connect(PT_INTRINSICS.PT_USER_GUEST, null, null);
            return ptGuestSession;
        }

		private void InitApplication(string appName)
		{
			String strServerConfigDir = ConfigPathResolver.GetOpenConfigPath();

			if(!"".Equals(appName))
			{
				appName = DEFAULT_APP_NAME;
			}

			IOKContext configContext = OKConfigFactory.createInstance(strServerConfigDir,appName);
			PortalObjectsFactory.Init(configContext);
		}
	}
}
